/**
 * 
 */
package com.faxsun.core.order.service.type;

import java.util.LinkedHashMap;

import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.order.service.type.OrderStatus;

/**
 * In order to translate order status and add new status,
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
public class FSOrderStatus extends OrderStatus {

	private static final LinkedHashMap<String, OrderStatus> TYPES = new LinkedHashMap<String, OrderStatus>();

	/**
	 * 
	 */
	private static final long serialVersionUID = 6759892203229656218L;
	public static final FSOrderStatus NAMED = new FSOrderStatus("NAMED",
			"Named");
	public static final FSOrderStatus QUOTE = new FSOrderStatus("QUOTE",
			"Quote");

	/**
	 * Represents a cart (non-submitted {@link Order}s)
	 */
	public static final FSOrderStatus IN_PROCESS = new FSOrderStatus(
			"IN_PROCESS", "购物车");

	/**
	 * Used to represent a completed {@link Order}. Note that this also means
	 * that the {@link Order} should have its {@link Order#getOrderNumber} set
	 */
	public static final FSOrderStatus SUBMITTED = new FSOrderStatus(
			"SUBMITTED", "已提交");
	public static final FSOrderStatus CANCELLED = new FSOrderStatus(
			"CANCELLED", "已取消");
	//
	// /**
	// * 已受理 
	// */
	 public static final FSOrderStatus CONFIRMED = new FSOrderStatus(
	 "CONFIRMED", "已受理");
	/**
	 * 已发货
	 */
	public static final FSOrderStatus DELIVERY = new FSOrderStatus("DELIVERY",
			"已发货");

	/**
	 * 已签收
	 */
	public static final FSOrderStatus SINGED = new FSOrderStatus("SIGNED",
			"已签收");


	/**
	 * Used when a CSR has locked a cart to act on behalf of a customer
	 */
	public static final FSOrderStatus CSR_OWNED = new FSOrderStatus(
			"CSR_OWNED", "Owned by CSR");

	public static FSOrderStatus getInstance(final String type) {
		return (FSOrderStatus) TYPES.get(type);
	}

	private String type;
	private String friendlyType;

	public FSOrderStatus() {
		// do nothing
	}

	public FSOrderStatus(final String type, final String friendlyType) {
		this.friendlyType = friendlyType;
		setType(type);
	}

	@Override
	public String getType() {
		return type;
	}

	@Override
	public String getFriendlyType() {
		return friendlyType;
	}

	private void setType(final String type) {
		this.type = type;
		if (!TYPES.containsKey(type)) {
			TYPES.put(type, this);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!getClass().isAssignableFrom(obj.getClass()))
			return false;
		FSOrderStatus other = (FSOrderStatus) obj;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

}
